package com.woniu.liuzhougov.authorityControl.service.impl;

import com.woniu.liuzhougov.authorityControl.dao.RoleMapper;
import com.woniu.liuzhougov.authorityControl.dao.RoleRightsMapper;
import com.woniu.liuzhougov.authorityControl.pojo.Role;
import com.woniu.liuzhougov.authorityControl.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author zhuxinpan
 * @ClassName RoleServiceImpl
 * @Description RoleService实现类
 * @Date 2019/4/26 10:01
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleRightsMapper roleRightsMapper;

    /**
     * @return java.util.List<com.woniu.liuzhougov.authorityControl.pojo.Role>
     * @author zhuxinpan
     * @date 2019/4/26 16:39
     * @description 列出所有角色
     */
    @Override
    public List<Role> listRoles(Integer currentPage, Integer pageSize) {
        return roleMapper.listRoles(currentPage, pageSize);
    }

    /**
     * @return int
     * @author zhuxinpan
     * @date 2019/4/26 16:39
     * @description 统计同名角色数量
     */
    @Override
    public int countNumberByRoleName(Role role) {
        return roleMapper.countNumberByRoleName(role);
    }

    /**
     * @return int
     * @author zhuxinpan
     * @date 2019/4/26 16:40
     * @description 添加角色
     */
    @Override
    public int addRole(Role role) {
        return roleMapper.insertRole(role);
    }

    /**
     * @return com.woniu.liuzhougov.authorityControl.pojo.Role
     * @author zhuxinpan
     * @date 2019/4/27 22:00
     * @description 获得角色
     */
    @Override
    public Role getRoleByRoleId(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    /**
     * @return int
     * @author zhuxinpan
     * @date 2019/4/27 22:29
     * @description 删除角色
     */
    @Override
    @Transactional
    public int deleteRoleByRoleId(Integer roleId) {
        int result = roleRightsMapper.deleteRoleRightsByRoleId(roleId);
        result += roleMapper.deleteByPrimaryKey(roleId);
        return result;
    }

    /**
     * @return java.util.List<com.woniu.liuzhougov.authorityControl.pojo.Role>
     * @author zhuxinpan
     * @date 2019/4/28 15:03
     * @description 列出所有角色
     */
    @Override
    public List<Role> listRoles() {
        return roleMapper.listRoles();
    }

    /**
     * @param role
     * @return int
     * @author zhuxinpan
     * @date 2019/4/28 23:44
     * @description 编辑角色
     */
    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKey(role);
    }


}
